/**
 * 
 */
package com.jolestar.yayaweibotong.task;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import org.apache.commons.lang.StringUtils;

import com.google.appengine.api.labs.taskqueue.Queue;
import com.google.appengine.api.labs.taskqueue.QueueFactory;
import com.google.appengine.api.labs.taskqueue.TaskOptions;
import com.google.appengine.api.labs.taskqueue.TaskOptions.Method;
import com.jolestar.yayaweibotong.dao.AccountDomain;
import com.jolestar.yayaweibotong.dao.StatusDomain;
import com.jolestar.yayaweibotong.dao.UserDomain;

/**
 * @author jolestar@gmail.com
 * 
 */
public class UpdateUserTask extends AbstractTask {

	public static final String QUENE_NAME = "user-queue";

	/**
	 * @param queueName
	 */
	public UpdateUserTask() {
		super(QUENE_NAME);
	}

	@Override
	protected boolean doTask(Map<String, String> headers,Map<String, String> params) {
		String email = params.get("email");
		if (StringUtils.isBlank(email)) {
			return true;
		}
		UserDomain user = userService.getUser(email);
		if (user == null) {
			log.warning("can not find user by email:" + email);
			return true;
		}
		switch (user.getSyncType()) {
		case MasterSlave:
			this.masterSlave(user);
			break;
		case EachOther:
			this.eachOther(user);
			break;
		}
		user.setSynced(true);
		user.save();
		return true;
	}

	private void masterSlave(UserDomain user) {
		AccountDomain masterAccount = user.getMasterAccount();
		if (masterAccount.isSynced()) {
			return;
		}
		List<StatusDomain> statuses = userService.getUnSyncedStatuses(
				masterAccount, 10);
		if (statuses.isEmpty()) {
			return;
		}
		List<AccountDomain> slaveAccounts = user.getSlaveAccounts();
		for (AccountDomain account : slaveAccounts) {
			if (!account.isActivity() || account.isSuspend()) {
				continue;
			}
			for (StatusDomain status : statuses) {
				Queue queue = QueueFactory
						.getQueue(UpdateStatusTask.QUENE_NAME);
				try {
					queue.add(TaskOptions.Builder.param("account",
							account.getKeyName()).param("status",
							status.getKeyName()));
					status.setSynced(true);
					status.setSyncedAt(new Date());
					status.save();
				} catch (Exception e) {
					log.severe("add to " + UpdateStatusTask.QUENE_NAME
							+ " quene error: account:" + account.getKeyName()
							+ ",status:" + status.getKeyName());
					log.log(Level.SEVERE, e.getMessage(), e);
				}
			}
			account.setSynced(true);
			account.setLastSyncStatusId(null);
			account.increaseSyncToCount(statuses.size());
			account.save();
		}
	}

	private void eachOther(UserDomain user) {
		List<AccountDomain> accounts = user.getAllAccounts();
		if (accounts.size() < 2) {
			return;
		}
		for (AccountDomain account : accounts) {
			if (!account.isActivity() || account.isSuspend()) {
				continue;
			}
			List<StatusDomain> statuses = userService.getUnSyncedStatuses(
					account, 10);
			if (statuses.isEmpty()) {
				continue;
			}
			for (AccountDomain otherAccount : accounts) {
				if (account == otherAccount) {
					continue;
				}
				if (!otherAccount.isActivity() || otherAccount.isSuspend()) {
					continue;
				}
				for (StatusDomain status : statuses) {
					Queue queue = QueueFactory
							.getQueue(UpdateStatusTask.QUENE_NAME);
					try {
						queue.add(TaskOptions.Builder.method(Method.POST)
								.param("status", status.getKeyName())
								.param("account", otherAccount.getKeyName()));
						status.setSynced(true);
						status.setSyncedAt(new Date());
						status.save();
					} catch (Exception e) {
						log.severe("add to " + UpdateStatusTask.QUENE_NAME
								+ " quene error: account:"
								+ otherAccount.getKeyName() + ",status:"
								+ status.getKeyName());
						log.log(Level.SEVERE, e.getMessage(), e);
					}
				}
				otherAccount.setSynced(true);
				otherAccount.save();
			}
		}
	}

}
